﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

namespace PengLuaKit
{
    //
    // @ PLKGame: a framework core manager
    //
    public class PLKGame : MonoSingleton<PLKGame>
    {
        protected override void InitOnAwake()
        {
            gameObject.AddComponent<TickManager>();
            gameObject.AddComponent<xLuaManager>().OnLuaPrepareWellHandler = OnStartGameHandler;

            PLKEventDispatcher.AddCSharpEventListener("SendFromLuaEvent", OnLuaEventHanddler);
        }

        private void OnLuaEventHanddler(System.Object eventData)
        {
            Debug.Log("From lua, eventData=" + eventData);
        }

        private void OnStartGameHandler()
        {
            // excute main.lua
            xLuaManager.Instance.DoFile("Main.lua");

            LuaFunction main = xLuaManager.Instance.GetFunction("Main");
            main.Call();
            main.Dispose();
            main = null;


            // send event
            Invoke("SendUnityEvent", 1.0f);
        }

        private void SendUnityEvent()
        {
            PLKEventDispatcher.DispatcherEvent("SendFromUnityEvent", "to lua");
        }


    }
}